package com.yanxinim.cn.store;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.yanxinim.cn.R;
import com.yanxinim.cn.util.ImageLoadTool;

import java.util.List;


/**
 * 店铺详情中商品信息
 */
public class StoreGoodsAdapter extends RecyclerView.Adapter<StoreGoodsAdapter.ViewHolder> {

    private Context mContext;
    private List<Goods> goodsTypeItemsInfos;
    private AdapterOnClickListener adapterOnClickListener;
    private int type;

    public AdapterOnClickListener getAdapterOnClickListener() {
        return adapterOnClickListener;
    }

    public void setAdapterOnClickListener(AdapterOnClickListener adapterOnClickListener) {
        this.adapterOnClickListener = adapterOnClickListener;
    }

    public StoreGoodsAdapter(Context mContext, List<Goods> goodsTypeItemsInfos, int type) {
        this.mContext = mContext;
        this.goodsTypeItemsInfos = goodsTypeItemsInfos;
        this.type = type;
    }

    public void addAllData(List<Goods> goodsItems) {
        goodsTypeItemsInfos.addAll(goodsItems);
        notifyItemRangeChanged(goodsTypeItemsInfos.size() - goodsItems.size(), goodsItems.size());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.store_goods_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") int i) {
        Goods goods = goodsTypeItemsInfos.get(i);
        ImageLoadTool.imageLoad(mContext, goods.getProductLogo(), viewHolder.goodsImg);
        viewHolder.goodsName.setText(goods.getProductName());
        viewHolder.goodsPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        viewHolder.goodsPrice.setText(String.format("Y%s", goods.getProductMoney()));

        if (!TextUtils.isEmpty( goods.getProductOrgiMoney()) ){
            if (!goods.getProductOrgiMoney().equals("null") ){
                viewHolder.goodsMarketPrice.setText(String.format("￥%s", goods.getProductOrgiMoney()));
            }else {
                viewHolder.goodsMarketPrice.setText(String.format("￥%s", goods.getProductMoney()));
            }
        }else {
            viewHolder.goodsMarketPrice.setText(String.format("￥%s", goods.getProductMoney()));
        }

        if (type == 1) {
            viewHolder.goodsDes.setVisibility(View.VISIBLE);
            viewHolder.tvGoodSales.setText(String.format("已售：%s", goods.getProductSellnum()));
            viewHolder.tvGoodsFlag.setText(goods.getProductWeight());
            viewHolder.goodsDes.setText(goods.getProductText());
        } else {
            viewHolder.tvGoodSales.setText(String.format("x%s", goods.getProductNum()));
            viewHolder.tvGoodsFlag.setText(String.format("规格：%s", goods.getProductWeight()));
            viewHolder.goodsDes.setVisibility(View.GONE);
        }
        viewHolder.itemView.setOnClickListener(v -> adapterOnClickListener.onClick(i, goods));
    }

    public interface AdapterOnClickListener {
        void onClick(int position, Goods data);
    }

    @Override
    public int getItemCount() {
        return goodsTypeItemsInfos.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView goodsImg;
        TextView goodsName;
        TextView goodsDes;
        TextView goodsPrice;
        TextView goodsMarketPrice;
        TextView tvGoodsFlag;
        TextView tvGoodSales;

        public ViewHolder(View itemView) {
            super(itemView);
            goodsImg = itemView.findViewById(R.id.goods_img);
            goodsName = itemView.findViewById(R.id.goods_name);
            goodsPrice = itemView.findViewById(R.id.goods_price);
            goodsDes = itemView.findViewById(R.id.tv_goods_des);
            goodsMarketPrice = itemView.findViewById(R.id.goods_market_price);
            tvGoodsFlag = itemView.findViewById(R.id.tv_goods_flag);
            tvGoodSales = itemView.findViewById(R.id.tv_goods_sales);
        }
    }
}
